﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.Entities;
using SD.IBUS;
using SD.BUS;

namespace SD.UI
{
    public partial class SearchBussinessType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void btnSearchAddress_Click(object sender, EventArgs e)
        {
            IReferenceDataBIZ iReferenceDataBIZ = new ReferenceDataBIZ();
            DataTable dt = iReferenceDataBIZ.ListRefByCodition(tbxBussinessName.Text, tbxSICCode.Text);

            if (dt.Rows.Count > 0)
            {
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                lbMessage.Text = string.Empty;
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                gvSearchAddress.Rows[0].Visible = false;
                lbMessage.Text = "No records found";
            }
        }

        protected void btnNone_Click(object sender, EventArgs e)
        {

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            tbxSICCode.Text = "";
            tbxBussinessName.Text = "";
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {

        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string selectValue = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[2].Text;//Request.Form["rdbChoose"];
            string contactValue = gvSearchAddress.SelectedValue.ToString();//Request.Form["contactID"]; 
            string sicCode = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[3].Text;//Request.Form["contactID"];  
            string str = "var Str = '" + selectValue + "';var ctID='" + contactValue + "';var vsiccode='" + sicCode + "';"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxSICCode').value=vsiccode;"+
                " window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxTypeOfBussiness').value=Str;"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_txtBussinessID').value=ctID;"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfBussiness').value=Str;"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfSicCode').value=vsiccode;window.close();";
            ScriptManager.RegisterClientScriptBlock(btnSelect, typeof(Button), "select", str, true);

        }

        private void LoadData()
        {
            IReferenceDataBIZ iReferenceDataBIZ = new ReferenceDataBIZ();
            DataTable dt = iReferenceDataBIZ.ListAll();

            if (dt.Rows.Count > 0)
            {
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                lbMessage.Text = string.Empty;
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.Rows[0].Visible = false;
                gvSearchAddress.DataBind();
                lbMessage.Text = "No records found";
            }
        }

        protected void gvSearchAddress_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSearchAddress.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void rdbChoose_CheckedChanged(object sender, EventArgs e)
        {
            Deselect();
            RadioButton rb = (RadioButton)sender;
            rb.Checked = true;
            GridViewRow row = (GridViewRow)((RadioButton)sender).NamingContainer;

            if (row != null)
            {
                gvSearchAddress.SelectedIndex = row.RowIndex;
            }
        }
        private void Deselect()
        {
            //GridViewRow row = new GridViewRow();
            int i = 0;
            foreach (GridViewRow r in gvSearchAddress.Rows)
            {
                RadioButton rb = (RadioButton)(gvSearchAddress.Rows[i].FindControl("rdbChoose"));
                rb.Checked = false;
                i++;

            }
        }
    }
}
